package com.fujitsu.hope.web.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.tkym.labs.store.BeanstoreException;

import com.fujitsu.hope.web.client.IrcService;
import com.fujitsu.hope.web.client.bean.Irc;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class IrcServiceImpl extends RemoteServiceServlet implements IrcService{
	
	public List<Irc> queryMessage(String arg) throws Exception {
		return new IrcStub().query(arg);
	}
	
	private class IrcStub{
		
		List<Irc> query(String channel) throws BeanstoreException{
			List<Irc> result = new ArrayList<Irc>();
			for(int i=0; i<100; i++)
				result.add(dummy(i));
			return result;
		}
		
		private Irc dummy(int id){
			Irc bean = new Irc();
			bean.setId((long)id);
			bean.setChannel("#Ch"+id%2);
			bean.setNickname("user"+id%10);
			StringBuilder sb = new StringBuilder();
			for(int i=0; i<(id%20+1); i++){
				sb.append("message");
			}
			bean.setMessage(sb.toString()+id%3);
			Date date = new Date((long)(new Date().getTime())+3600000*id);
			bean.setTime(date);
			return bean;
		}
	}
	
//	private static final String DB_LOC = "d:\\irc.db";
//	@SuppressWarnings("unused")
//	private class IrcDao{
//		List<Irc> query(String channel) throws BeanstoreException{
//			BeanstoreService service = BeanstoreServiceProvider.getInstance().get(DB_LOC);
//			if(channel == null || channel.length()==0){
//				return service.query(Irc.class)
//					.sort(property("id").asc())
//					.asList();
//			}else{
//				return service.query(Irc.class)
//					.filter(property("channel").equalsTo(channel))
//					.sort(property("id").asc())
//					.asList();
//			}
//		}
//	}
}